import React from 'react';
import apisauce from 'apisauce';
import Toast from '../../utils/Toast';
import { Loading } from '../../utils/Loading';

export default class HttpRequestManager {
    static _instance
    _api

    constructor() {
        this._api = createApi()
    }

    static getInstance() {
        if (!this._instance) {
            //console.log("not exist, new ......")
            this._instance = new HttpRequestManager()
        } else {
            //console.log("exist, user *******")
        }

        return this._instance
    }

    static getApi() {
        return this.getInstance()._api
    }

    /**
     * get 请求
     * @param {请求路径} path 
     * @param {get参数} params 
     * @returns 
     */
    get(path: string, params: any) {
        Loading.show()
        var p = new Promise(function (resolve, reject) {
            let api = HttpRequestManager.getApi()
            api.get(path, params).then((response) => {
                Loading.hidden()
                onHttpResponse(response, resolve, reject)
            })
        });
        return p
    }

    /**
     * post 请求
     * @param {请求路径} path 
     * @param {post参数} params 
     * @returns 
     */
    post(path: string, params: any) {
        Loading.show()
        var p = new Promise(function (resolve, reject) {
            let api = HttpRequestManager.getApi()
            api.post(path, params).then((response) => {
                Loading.hidden()
                onHttpResponse(response, resolve, reject)
            })
        });
        return p
    }

}

const baseHeader = {
    Accept: 'application/json',
    'Content-Type': 'application/json',
}

function createApi() {
    api = apisauce.create({
        baseURL: 'http://shuitupaycallbacktest.callme.work',
        headers: baseHeader,
        timeout: 15000
    })

    api.addAsyncResponseTransform(async response => {
        if (response.ok) {
            //http code:200 
            //return response.data
            if (response.data.code == 1) {
                Toast.show(response.data.message)
            } else {
                return response.data.data
            }
        } else {
            if (response.status === 401) {
                Toast.show("Token失效,请重新登录")
                // refresh token
                // jemp login
            } else {
                if (response.problem === '') {
                    Toast.show("数据加载失败")
                } else {
                    Toast.show(response.problem)
                }
            }
        }
        //return response
    });

    return api
}


//处理回调
function onHttpResponse(response, resolve, reject) {
    if (response.ok) {
        if (response.data.code == 0) {
            resolve(response.data.data)
        } else {
            reject({
                code: response.data.code,
                msg: response.data.message
            });
        }
    } else {
        reject({
            code: response.status,
            msg: response.problem ? response.problem : "数据加载失败"
        });
    }
}